//
//  MagazineViewController.m
//  WZ66WZZB
//
//  Created by Sunny on 11-5-11.
//  Copyright 2011 SUN. All rights reserved.
//

#import "MagazineViewController.h"

#import "WZ66WZZBAppDelegate.h"
#import "MainViewController.h"
#import "ContentViewController.h"

#import "Magazine.h"
#import "UIImageExtras.h"

@implementation MagazineViewController

@synthesize imageDownloadsInProgress;

#pragma mark -
#pragma mark @protocol AFOpenFlowViewDataSource Method
- (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex:(int)index{
	//TODO
}

- (UIImage *)defaultImage{
	return [UIImage imageNamed:@"default.jpg"];
}

#pragma mark -
#pragma mark @protocol AFOpenFlowViewDelegate Method
- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index{
	//TODO
}

- (void)openFlowView: (AFOpenFlowView *)openFlowView imageSelected:(int)index {
    
    MainViewController *mainViewController = [WZ66WZZBAppDelegate sharedAppDelegate].mainViewController;
	Magazine *magazine = [mainViewController.magazineArray objectAtIndex:index];
    
    [mainViewController httpMagazineInit];
	[mainViewController httpBlogXml:magazine.bloglistUrl];
    
    ContentViewController *contentViewController = [WZ66WZZBAppDelegate sharedAppDelegate].contentViewController;
    [mainViewController dismissModalViewControllerAnimated:NO];
    [mainViewController presentModalViewController:contentViewController animated:NO];
    
    [contentViewController.activityIndicator startAnimating];
    
}



#pragma mark -
#pragma mark init & dataLoad Method
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    if (self.imageDownloadsInProgress == nil) {
        self.imageDownloadsInProgress = [[[NSMutableDictionary alloc] init] autorelease];
    }
	[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"MainBackground.jpg"]]];
    [super viewDidLoad];
}

- (void)dataLoad {
	MainViewController *mainViewController = [WZ66WZZBAppDelegate sharedAppDelegate].mainViewController;
	NSMutableArray *magazineArray = mainViewController.magazineArray;
	
	[(AFOpenFlowView *)self.view setNumberOfImages:[magazineArray count]];
	int i=0;
	for (Magazine *magazine in magazineArray) {
		UIImageView *imageView = [[UIImageView alloc] init];
		imageView.image = [UIImage imageNamed:@"MagazineDefault.jpg"];
		NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
		[self startImageDownload:magazine.coverImageUrl forIndexPath:indexPath drawingView:imageView];
		[(AFOpenFlowView *)self.view setImage:[imageView.image cropCenterAndScaleImageToSize:CGSizeMake(320, 420)] forIndex:i];
		[indexPath release];
		[imageView release];
		i++;
	}
}

#pragma mark -
#pragma mark Lazyload Image Method
- (void)startImageDownload:(NSString *)imageUrl forIndexPath:(NSIndexPath *)indexPath drawingView:(UIImageView *) imageView
{
	ImageDownloader *imageDownloader = [self.imageDownloadsInProgress objectForKey:indexPath];
	
	if (imageDownloader == nil) 
    {
        imageDownloader = [[ImageDownloader alloc] init];
		
		imageDownloader.indexPath = indexPath;
		imageDownloader.imageUrl = imageUrl;
		imageDownloader.imageView = imageView;
        imageDownloader.delegate = self;
		imageDownloader.imageDownloadsInProgress = self.imageDownloadsInProgress;
        [self.imageDownloadsInProgress setObject:imageDownloader forKey:indexPath];
        [imageDownloader startDownload];
        [imageDownloader release];
    }
}

// called by our ImageDownloader when an icon is ready to be displayed
- (void)appImageDidLoad:(NSIndexPath *)indexPath drawingView:(UIImageView *) imageView
{
	ImageDownloader *imageDownloader = [self.imageDownloadsInProgress objectForKey:indexPath];
	
	if (imageDownloader != nil)
    {
        imageView.image = imageDownloader.downloadImage;
		
    }
	[(AFOpenFlowView *)self.view setImage:[imageView.image cropCenterAndScaleImageToSize:CGSizeMake(320, 420)] forIndex:indexPath.row];
	[imageDownloader release];
}


#pragma mark -
#pragma mark Other Method
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if((interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (interfaceOrientation == UIDeviceOrientationLandscapeRight)){
		return YES;
	}
	else {
		return NO;
	}
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[imageDownloadsInProgress release];
    [super dealloc];
}

@end
